package org.standard.sys.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import org.standard.base.model.BaseRole;
import org.standard.base.service.BaseRoleService;
import org.standard.commons.struts2.BaseAction;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.sys.model.SysAsset;
import org.standard.sys.model.SysFunc;
import org.standard.sys.model.SysMenu;
import org.standard.sys.service.SysAssetService;
import org.standard.sys.service.SysFuncService;
import org.standard.sys.service.SysMenuService;

public class SysAssetAction extends BaseAction{
	
	private SysAssetService sysAssetService;
	
	private SysFuncService sysFuncService;
	
	private BaseRoleService baseRoleService;
	
	private SysMenuService sysMenuService;
	
	private FlipPageInfo pageData = new FlipPageInfo();
	
	private SysAsset sysAsset;
	
	private List<SysFunc> sysFuncs;
	
	private List<SysMenu> sysMenus;
	
	private List<BaseRole> roles;
	
	private List<SysAsset> sysAssets;
	
	private List list;
	
	private BaseRole baseRole;
	
	private String funcName;
	
	private String[] ids;
	
	private String[] role;
	
	private String[] asset;
	
	
	
	public String assetIndex(){
		
		return "assetIndex";
	}
	
	
	public String assetTop(){
		
		
		return "assetTop";
	}
	
	public String assetLeft(){
		
		roles = baseRoleService.roleList();
		
		return "assetLeft";
	}
	
	public String assetManager(){
		
		list = sysAssetService.listSysAsset();
		return "assetManager";
	}
	
	public String showRole(){
		
		baseRole = baseRoleService.findByRoleId(baseRole.getRoleId());
		
		String assetIdGroup = baseRole.getAssetIdGroup();
		
		if(StringUtils.isNotBlank(assetIdGroup)){
			
			assetIdGroup = assetIdGroup.substring(0,assetIdGroup.length()-1);
			
			asset = assetIdGroup.split(",");
		}
		
		return assetManager();
	}
	
	
	public String chooseFunc(){
		
		sysMenus = sysMenuService.sysMenuList();
		
		return "chooseFunc";
	}
	
	
	
	public String index(){
		
		pageData = sysAssetService.listSysAsset(pageData);
		
		System.out.println(pageData.getData().size());
		
		return "listSysAsset";
	}
	
	
	public String preCreateSysAsset(){
		
		return "createSysAsset";
	}
	
	public String createSysAsset(){
		
		char s;
		try {
			s = sysAssetService.createSysAsset(sysAsset);
			if(s == '1'){
				
				info = "asset_1";//成功
				
				return "createSysAsset_succ";	
			}else if(s == '2'){
				
				info = "asset_2";//请选择功能点
				return "createSysAsset_fail";
			}else if(s == '3'){
				info = "asset_3";//资源名称不能为空
				return "createSysAsset_fail";
			}else if(s == '4'){
				info = "asset_4";//资源英文名称不能为空
				return "createSysAsset_fail";
			}else if(s == '5'){
				info = "asset_5";//资源链接不能为空
				return "createSysAsset_fail";
			}else {
				info = "asset_0";//失败
				return "createSysAsset_fail";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			info = "asset_0";//失败
			return "createSysAsset_fail";
		}
		
		
	}
	
	public String preModifySysAsset(){
		
		sysAsset = sysAssetService.findByAssetId(sysAsset.getAssetId());
		
		SysFunc sysFunc = sysFuncService.findFuncById(sysAsset.getFuncId());
		
		funcName = sysFunc.getFuncName();
		
		return "modifySysAsset";
	}
	
	public String modifySysAsset(){
		
		char s;
		try {
			s = sysAssetService.modifySysAsset(sysAsset);
			if(s == '1'){
				
				info = "asset_1";//成功
				return "modifySysAsset_succ";
			}else if(s == '2'){
				
				info = "asset_2";//请选择功能点
				return "modifySysAsset_fail";
			}else if(s == '3'){
				info = "asset_3";//资源名称不能为空
				return "modifySysAsset_fail";
			}else if(s == '4'){
				info = "asset_4";//资源英文名称不能为空
				return "modifySysAsset_fail";
			}else if(s == '5'){
				info = "asset_5";//资源链接不能为空
				return "modifySysAsset_fail";
			}else {
				info = "asset_0";//失败
				return "modifySysAsset_fail";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			info = "asset_0";//失败
			return "modifySysAsset_fail";
		}
		
		
	}
	
	public String batchDeleteSysAsset(){
		
		char s;
		try {
			s = sysAssetService.bacthDeleteSysAsset(ids);
			if(s == '1'){
				
				info = "asset_1";//成功
			}else if(s == '0'){
				info = "asset_0";//失败
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			info = "asset_0";//失败
		}

		
		return "batchDeleteSysAsset_succ";
	}
	
	public String deleteSysAsset(){
		
		char s;
		try {
			s = sysAssetService.deleteSysAsset(sysAsset);
			
			if(s == '1'){
				
				info = "asset_1";//成功
			}else if(s == '0'){
				info = "asset_0";//失败
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			info = "asset_0";//失败
		}
		
		return "deleteSysAsset_succ";
	}
	
	public String preSetAssetIdGroup(){
		
		return assetIndex();
	}
	
	public String setAssetIdGroup(){
		
		String funcIdGroup = "";
		if(asset!= null && asset.length>0){
			for(int j=0;j<asset.length;j++){
				funcIdGroup += asset[j]+",";
			}
		}
		
		if(role != null && role.length>0){
			for(int i=0;i<role.length;i++){
				String roleId = role[i];
				baseRole = baseRoleService.findByRoleId(roleId);
				baseRole.setAssetIdGroup(funcIdGroup);
				
				baseRoleService.modifyRole(baseRole);
			}
			
			info = "asset_1";//成功
		}else{
			
			info = "asset_6";//请选择角色
		}
		
		return assetManager();
	}


	public FlipPageInfo getPageData() {
		return pageData;
	}


	public void setPageData(FlipPageInfo pageData) {
		this.pageData = pageData;
	}


	public SysAsset getSysAsset() {
		return sysAsset;
	}


	public void setSysAsset(SysAsset sysAsset) {
		this.sysAsset = sysAsset;
	}


	public List<SysFunc> getSysFuncs() {
		return sysFuncs;
	}


	public void setSysFuncs(List<SysFunc> sysFuncs) {
		this.sysFuncs = sysFuncs;
	}


	public List<SysAsset> getSysAssets() {
		return sysAssets;
	}


	public void setSysAssets(List<SysAsset> sysAssets) {
		this.sysAssets = sysAssets;
	}


	public void setSysAssetService(SysAssetService sysAssetService) {
		this.sysAssetService = sysAssetService;
	}


	public void setSysFuncService(SysFuncService sysFuncService) {
		this.sysFuncService = sysFuncService;
	}


	public String getFuncName() {
		return funcName;
	}


	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}


	public String[] getIds() {
		return ids;
	}


	public void setIds(String[] ids) {
		this.ids = ids;
	}


	public List<BaseRole> getRoles() {
		return roles;
	}


	public void setRoles(List<BaseRole> roles) {
		this.roles = roles;
	}


	public void setBaseRoleService(BaseRoleService baseRoleService) {
		this.baseRoleService = baseRoleService;
	}


	public List<SysMenu> getSysMenus() {
		return sysMenus;
	}


	public void setSysMenus(List<SysMenu> sysMenus) {
		this.sysMenus = sysMenus;
	}


	public BaseRole getBaseRole() {
		return baseRole;
	}



	public void setBaseRole(BaseRole baseRole) {
		this.baseRole = baseRole;
	}



	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}



	public String[] getRole() {
		return role;
	}



	public void setRole(String[] role) {
		this.role = role;
	}



	public String[] getAsset() {
		return asset;
	}

	public void setAsset(String[] asset) {
		this.asset = asset;
	}

}
